package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWork;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.LoginRequest;
import com.jjwxc.jwjskandriod.model.LoginResponse;
import com.jjwxc.jwjskandriod.model.VipDistanceResponse;
import com.jjwxc.jwjskandriod.readActivity.local.ReadSettingManager;
import com.jjwxc.jwjskandriod.readActivity.utils.SharedPreUtils;

public class LoginActivity extends BaseActivity {
    private TextView tv_login;

    private EditText ed_phone;
    private EditText ed_password;

    private CheckBox checkbox;
    private TextView tv_reg_one;
    private TextView tv_reg_two;
    private TextView tv_gophone;
    private ImageView img_show;
    private TextView tv_gopassword;
    private LinearLayout ll_fanhui;

    private boolean oneShow = false;

    @Override
    public int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    public void findView() {
        tv_login = findViewById(R.id.tv_login);
        ll_fanhui = findViewById(R.id.ll_fanhui);

        img_show = findViewById(R.id.img_show);
        ed_phone = findViewById(R.id.ed_phone);
        ed_password = findViewById(R.id.ed_password);
        checkbox = findViewById(R.id.checkbox);
        tv_reg_one = findViewById(R.id.tv_reg_one);
        tv_reg_two = findViewById(R.id.tv_reg_two);
        tv_gophone = findViewById(R.id.tv_gophone);
        tv_gopassword = findViewById(R.id.tv_gopassword);
        checkbox.setChecked(false);

    }

    @Override
    public void setListener() {
        ll_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_login.setOnClickListener(v -> {
            String phone = ed_phone.getText().toString();
            String password = ed_password.getText().toString();

            if (StUtils.isEmpty(phone)) {
                showToast("请输入手机号");
                return;
            }
            if (StUtils.isEmpty(password)) {
                showToast("请输入密码");
                return;
            }
            if (!checkbox.isChecked()) {
                showToast("请阅读并同意协议");
                return;
            }
            login(phone, password);

        });
        img_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (oneShow) {
                    oneShow = false;
                    img_show.setImageResource(R.mipmap.mima_false);
                    ed_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    oneShow = true;
                    img_show.setImageResource(R.mipmap.mima_true);
                    ed_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });
        tv_reg_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, WebActivity.class);
                intent.putExtra("WEB_URL", Url.registerUser);
                intent.putExtra("WEB_NAME", "用户注册协议");
                startActivity(intent);
            }
        });
        tv_reg_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, WebActivity.class);
                intent.putExtra("WEB_URL", Url.privacy);
                intent.putExtra("WEB_NAME", "用户隐私条款");
                startActivity(intent);
            }
        });
        tv_gophone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = ed_phone.getText().toString();
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                intent.putExtra("phone", phone);
                startActivity(intent);
            }
        });
        tv_gopassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = ed_phone.getText().toString();
                Intent intent = new Intent(LoginActivity.this, CheckPhoneAcitivity.class);
                intent.putExtra("phone", phone);
                startActivity(intent);
            }
        });
    }

    @Override
    public void afterCreate() {
        setNoTitle();
    }

    public void login(String phone, String password) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setMobile(phone);
        loginRequest.setPassword(password);
        loginRequest.setDeviceId(getIMEI());
        loginRequest.setDeviceName(android.os.Build.MODEL);
        loginRequest.setLoginType("mobile_password");
        post(Url.login, "", LoginResponse.class, new FFNetWorkCallBack<LoginResponse>() {
            @Override
            public void onSuccess(LoginResponse response) {
                if (response.getCode() == 200) {
                    PreUtils.setString("token", response.getData().getToken());
                    PreUtils.setString("Authorization", response.getData().getTokenHead() + response.getData().getToken());
                    PreUtils.setString("phone", phone);
                    PreUtils.setBoolean("tourist", false);
                    PreUtils.setString("RefreshToken", response.getData().getRefreshToken());
                    //获取VIP信息
                    getvipDistance();
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this,
                            MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    showToast("密码不正确");
                }

            }

            @Override
            public boolean onFail(LoginResponse response) {
                showToast("网络出现问题");
                return false;
            }
        }, JSON.toJSONString(loginRequest));
    }

    public void getvipDistance() {
        int readbg = SharedPreUtils.getInstance().getInt(ReadSettingManager.SHARED_READ_BG, 0);
        //获取是否是VIP
        new FFNetWork().post(Url.vipDistance, null, new FFNetWorkCallBack<VipDistanceResponse>() {
            @Override
            public void onSuccess(VipDistanceResponse response) {
                if (response.getCode() == 200) {
                    //不是VIP
                    if (response.getData() > 0) {
                        PreUtils.setInt("head_bg", 3);
                        PreUtils.setInt("app_bg", 3);
                        if (readbg >= 0 && readbg < 3) {
                            SharedPreUtils.getInstance().putInt(ReadSettingManager.SHARED_READ_BG, 3);
                        }
                    } else {
                        PreUtils.setInt("head_bg", 0);
                        PreUtils.setInt("app_bg", 0);
                        if (readbg > 3) {
                            SharedPreUtils.getInstance().putInt(ReadSettingManager.SHARED_READ_BG, 0);
                        }
                    }


                }
            }

            @Override
            public boolean onFail(VipDistanceResponse response) {
                return false;
            }
        }, VipDistanceResponse.class);

    }

    @SuppressLint("HardwareIds")
    public String getIMEI() {
        String id;
//        //android.telephony.TelephonyManager
        TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null) {
            id = mTelephony.getDeviceId();
        } else {
            //android.provider.Settings;
            id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return id;
    }


}
